package cn.pn.project.mapper.comment;

import cn.pn.project.common.Page;
import cn.pn.project.pojo.comment.Comment;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CommentMapper {
    /**
     * 查询全部评论（查询规则未知，用于发现/评论页面分页展示）
     * @return
     * @throws Exception
     */
    List<Comment> listAllComment(Page<Comment> page) throws Exception;

    /**
     * 根据评论id查询评论
     * @param commentId
     * @return
     * @throws Exception
     */
    @Select("SELECT * FROM `comment` where commentId=#{commentId};")
    Comment getCommentByCommentId(@Param("commentId") Integer commentId) throws Exception;

    /**
     * 根据商品id查询该商品的主评论数量（针对商品的评论，不计算子评论）
     * @param commodityId 商品ID
     * @return 主评论数量
     * @throws Exception
     */
    @Select("SELECT COUNT(1) FROM `comment` WHERE fatherId=0 AND `status`=1 AND commodityId=#{commodityId};")
    Integer mainCountByCommodityId(@Param("commodityId") Integer commodityId) throws Exception;

    /**
     * 根据商品id查询该商品的主评论集合并分页（无子评论）
     * @param commodityId 商品ID
     * @param page 分页
     * @return 主评论集合
     * @throws Exception
     */
    @Select("SELECT * FROM `comment` WHERE fatherId=0 AND `status`=1 AND commodityId=#{commodityId} ORDER BY commentTime DESC LIMIT #{page.beginPos},#{page.pageSize}")
    List<Comment> listMainComment(@Param("commodityId") Integer commodityId,@Param("page") Page page) throws Exception;


    /**
     * 根据商品ID查询主评论的子评论数量
     * @param commodityId 商品ID
     * @param belongToId 子评论所属主评论id（非父id）
     * @return 某主评论的子评论数量
     * @throws Exception
     */
    @Select("SELECT COUNT(1) FROM `comment` WHERE belongToId=#{belongToId} AND commodityId=#{commodityId} AND `status`=1;")
    Integer childCommentCount(@Param("belongToId") Integer belongToId,@Param("commodityId") Integer commodityId) throws Exception;

    /**
     * 根据商品ID查询主评论的子评论集合并分页
     * @param belongToId 子评论所属主评论id（非父id）
     * @param commodityId 商品ID
     * @return 某商品某主评论的子评论集合
     * @throws Exception
     */
    @Select("  SELECT * FROM `comment`" +
            "  WHERE belongToId=#{belongToId} AND commodityId=#{commodityId} AND `status`=1" +
            "  ORDER BY commentTime DESC" +
            "  LIMIT #{page.beginPos},#{page.pageSize}")
    List<Comment> listChildComment(@Param("belongToId") Integer belongToId,@Param("commodityId") Integer commodityId,@Param("page") Page page) throws Exception;


    /**
     * 根据主评论id+1个点赞
     * @param commentId 主评论id
     * @return 0 1
     * @throws Exception
     */
    @Update("UPDATE `comment` SET likeCount=likeCount+1 WHERE commentId=#{commentId};")
    Integer likeCountRise(Integer commentId) throws Exception;

    /**
     * 根据主评论id-1个点赞
     * @param commentId
     * @return
     * @throws Exception
     */
    @Update("UPDATE `comment` SET likeCount=likeCount-1 WHERE commentId=#{commentId};")
    Integer likeCountDown(Integer commentId) throws Exception;

    /**
     * 根据用户id和商品id查询该商品该用户是否评论过
     * @param userId 用户id
     * @param commodityId 商品id
     * @return 评论对象
     * @throws Exception
     */
    @Select("SELECT * FROM `comment` WHERE fatherId=0 AND userId=#{userId} AND commodityId=#{commodityId};")
    Comment isCommentedOn(@Param("userId") Integer userId,@Param("commodityId")Integer commodityId) throws Exception;

    /**
     * 新增评论
     * @param comment 评论对象
     * @return 0 1
     * @throws Exception
     */
    @Insert({"INSERT INTO `comment` VALUES(NULL,#{userId},#{fatherId}" +
            ",#{belongToId},#{commodityId},#{content}" +
            ",#{level},#{imgId},0,0,#{commentTime, jdbcType=TIMESTAMP}" +
            ",1,null,null,null); "})
    @Options(useGeneratedKeys = true, keyProperty = "commentId", keyColumn = "commentId")
    Integer addComment(Comment comment) throws Exception;

    /**
     * 根据评论id和图片id更新评论（只更新图片）
     * @param commentId 评论id
     * @param imgId 图片id
     * @return 0 1
     * @throws Exception
     */
    @Update("UPDATE `comment` SET imgId=#{imgId} WHERE commentId=#{commentId}")
    Integer updateImgIdByCommentId(@Param("commentId") Integer commentId,@Param("imgId")Integer imgId) throws Exception;

    /**
     * 更新评论（只更新内容、评分、图片）
     * @param comment 评论对象
     * @return 0 1
     * @throws Exception
     */
    @Update("UPDATE `comment` SET content=#{comment.content},`level`=#{comment.level},imgId=#{comment.imgId} WHERE commentId=#{comment.commentId}")
    Integer updateComment(@Param("comment") Comment comment) throws Exception;

    /**
     * 回复次数+1
     * @param commentId 主评论id，子评论不计算次数
     * @return
     * @throws Exception
     */
    @Update("UPDATE `comment` SET childCount=childCount+1 WHERE commentId=#{commentId};")
    Integer childCountRise(@Param("commentId") Integer commentId) throws Exception;

    /**
     * 回复次数-1
     * @param commentId 主评论id，子评论不计算次数
     * @return
     * @throws Exception
     */
    @Update("UPDATE `comment` SET childCount=childCount-1 WHERE commentId=#{commentId};")
    Integer childCountDown(@Param("commentId") Integer commentId) throws Exception;

    /**
     * 查询最大主键
     * @return
     * @throws Exception
     */
    @Select("SELECT MAX(commentId) FROM pn_base.comment;")
    Integer getMaxId() throws Exception;
}
